package com.googlecode.bizyap.core;

import java.security.Principal;
import java.util.HashSet;
import java.util.Set;

public class UserPrincipal implements Principal {

	private String name = null;
	private String domain = null;

	//real user principal
	private Principal appUser = null;
	//real user roles
	private Set<Principal> appRoles = null;;

	private UserPrincipal(String name, String domain) {
		super();
		this.name = name;
		this.domain = domain;
	}

	public String getName() {
		return name;
	}

	public String getDomain() {
		return domain;
	}

	public Principal getAppUser() {
		return appUser;
	}

	public void setAppUser(Principal user) {
		this.appUser = user;
	}

	public Set<Principal> getAppRoles() {
		return appRoles==null ? new HashSet<Principal>() : appRoles;
	}

	public void setAppRoles(Set<Principal> roles) {
		if(roles==null) {
			roles = new HashSet<Principal>();
			return;
		}
		this.appRoles = roles;
	}

	public static UserPrincipal createInstance(String username){
		String[] parts = username.split("\\@");
		String name = username;
		String domain = null;

		if(parts!=null && parts.length==2){
			name = parts[0];
			domain = parts[1];
		}
		return new UserPrincipal(name, domain);
	}
}
